/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/* IDataGroupBrowser.m */
#import <appkit/NXBrowser.h>
#import <appkit/Matrix.h>
#import <appkit/Text.h>
#import <libc.h>
#import <strings.h>
#import "IDataGroupBrowser.h"
#import "IItemHeaderBrowser.h"
#import "INXBrowserCellWithLinkedObject.h"
#import "INntpIO.h"
#import "INewsgroupInfoD.h"
#import "ITreeNodeD.h"
#import "errdebug.h"
#import "data_types.h"

#define 	MAXVISIBLECOL 		4
#define 	MINCOLWIDTH		80

@implementation IDataGroupBrowser

- initFrame:(NXRect *)frameRect
{
    [super initFrame:frameRect];
    [self setMinColumnWidth:MINCOLWIDTH];
    [self setMaxVisibleColumns:MAXVISIBLECOL];
    [self acceptArrowKeys:YES andSendActionMessages:NO];
    
    iKeyRecord[0] = '\0';
    return self;
}

/*
    The target object and action message of newsBrowser is IDataGroupBrowser
    and NewsGroupBrowserLeafHandler.  I.e., NewsGroupBrowserLeafHandler is
    called when a newsgroup is selected.
*/

- (void)leafHandler:sender
{
    id		selectedCell;
    id		node;
    id		itemHeaderslist;
    // valuables for routine of updating cells
    id		cellList, bcell, linkednode, groupInfo;
    int		i;

    iKeyRecord[0] = '\0';		// reset key record
    [window makeFirstResponder:self];	// set first responder to self
    
    selectedCell = [[self matrixInColumn:[self lastColumn]] selectedCell];

    if ([selectedCell isLeaf]) {
        node = [selectedCell node];
        itemHeaderslist = [iIOmodule itemHeadersOf:node];
	
	// this part is for updating unmarked image on each cell
	//    but,... it's very timecomsuming.
	DBG(10,fprintf(stderr," +++ update cells in browser +++\n"));
	cellList = [[self matrixInColumn:[self lastColumn]] cellList];
	for (i=0; bcell=[cellList objectAt:i]; ++i) {
	    if ([bcell isLeaf] && (linkednode=[bcell node]) != nil) {
		if ((groupInfo=[[linkednode linkedData] dataForKey:GROUPINFO])
								!= nil) {
		    DBG(10,fprintf(stderr,"  newsgroup = %s\n",
		    		(char *)[groupInfo infoForKey:"Newsgroups"]));
		    if ([groupInfo isAllArticleMarked] == NO) {
			[bcell setImage:[NXImage findImageNamed:UNREADMARK]];
			DBG(10,fprintf(stderr,"    ooo unread mark ooo\n"));
		    } else {
			[bcell setImage:nil];
			DBG(10,fprintf(stderr,"    xxx nil xxx\n"));
		    }
		}
	    }
	}

	[self displayColumn:[self lastColumn]]; // update cell
	DBG(10,fprintf(stderr," --- update cells in browser ---\n"));
	
	[iItemRepUIF setRootListnode:itemHeaderslist];
        [iItemRepUIF loadColumnZero];
    } else {
	[iItemRepUIF setRootListnode:nil];
        [iItemRepUIF loadColumnZero];
    }
}

- directoryHandler:node
{
    // called from ITreeBrowser
    // return the list of nodes, the list will be used in ITReeBrowser
    // to fill up the cells under this directory

    iKeyRecord[0] = '\0';		// reset key record
    [window makeFirstResponder:self];	// set first responder to self
    
    return [iIOmodule subDirectoryOf:node];
}

- setONewsgroupScroll:anObject
{
    oNewsgroupScroll = anObject;
    [self setAuxAgent:oNewsgroupScroll];
    [self setAuxAction:@selector(newsgroupClicked:)];
    return self;
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- keyDown:(NXEvent *)theEvent
{
    int		ilen;
    unsigned int	ch;
    
    // CR: perform click
    // alpha numeric: select matched sell
    
    DBG(1,fprintf(stderr,"  keydown"));
    if ((ch=(theEvent->data).key.charCode) == NX_CR) {
	// "Return" key pressed
	DBG(1,fprintf(stderr,"  NX_CR"));
	// click selected cell
	[[self matrixInColumn:[self selectedColumn]] sendAction];
	iKeyRecord[0]='\0';	// reset key record
	return self;
    }
    if (ch == 0x15) {
	// produced by 'control+u'
	// set path to root
	iKeyRecord[0] = pathSeparator;
	iKeyRecord[1] = '\0';
	[self setPath:iKeyRecord];
	iKeyRecord[0] = '\0';		// reset iKeyRecord
	return self;
    }

    // set path for browser
    if ((theEvent->data).key.charSet == NX_ASCIISET &&
    		0x20 <= ch && ch <= 0x7f &&
		(ilen=strlen(iKeyRecord)) <= 31) {
	if (ch == NX_DELETE) {
	    if ((ilen = ilen - 1) < 0) {
		ilen = 0;
	    }
	    iKeyRecord[ilen] = '\0';
	} else {
	    iKeyRecord[ilen] = ch;
	    iKeyRecord[ilen+1] = '\0';
	}
	DBG(1,fprintf(stderr,"  iKeyRecord=%s\n", iKeyRecord));
	[self setPath:iKeyRecord];
	return self;
    }
    
    // handle arrow keys in super class
    return ([super keyDown:theEvent]);
}

@end
